// 封装购物车模块

import { defineStore } from 'pinia'
import {computed, ref} from 'vue'
import {userStore} from "@/stores/user.js";
import {delCartAPI, findNewCartListAPI, insertCartAPI} from "@/apis/cart.js";

export const useCartStore = defineStore('cart', () => {
    const storeUser = userStore();
    // 1. 定义state - cartList
    const cartList = ref([])

    const isLogin=computed(()=>storeUser.user.token)

    // 获取最新购物车列表action
    const updateNewList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result
    }

    // 2. 定义action - addCart
    const addCart =  async (goods) => {
        if (isLogin.value) {
            const {skuId, count} = goods;
            //调后端接口插入
            await insertCartAPI({skuId, count});
            //调后端接口获取列表
            updateNewList();
        } else {
            // 添加购物车操作
            // 已添加过 - count + 1
            // 没有添加过 - 直接push
            // 思路：通过匹配传递过来的商品对象中的skuId能不能在cartList中找到，找到了就是添加过
            const item = cartList.value.find((item) => goods.skuId === item.skuId);
            if (item) {
                // 找到了
                item.count += goods.count;
            } else {
                // 没找到
                cartList.value.push(goods)
            }
        }

    }

    const delCart = async (skuId) => {

        if (isLogin.value) {
            await delCartAPI([skuId]);
            //调后端接口获取列表
            updateNewList();
        } else {
            // 思路：
            // 1. 找到要删除项的下标值 - splice
            // 2. 使用数组的过滤方法 - filter
            const idx = cartList.value.findIndex((item) => skuId === item.skuId);
            cartList.value.splice(idx, 1)
        }


    }

    //单选
    const singleCheck=(skuId,selected)=>{
        const item = cartList.value.find((item) => skuId === item.skuId);
        if (item) {
            item.selected = selected;
        }
    }
    //全选
    const allCheck = (selected) => {
        // 把cartList中的每一项的selected都设置为当前的全选框状态
        cartList.value.forEach(item => item.selected = selected)
    }

    const clearCart = () => {
        cartList.value = [];
    }

    // 是否全选计算属性
    const isAll = computed(() => cartList.value.every((item) => item.selected))

    //选中的数量和金额
    const selectCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0));
    const selectPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))

    // 计算属性
    // 1. 总的数量 所有项的count之和,a上一次调用的值,c当前值,0是初始值
    const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
    // 2. 总价 所有项的count*price之和
    const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))
    return {
        cartList,
        addCart,
        allCount,
        allPrice,
        delCart,
        singleCheck,
        allCheck,
        isAll,
        selectCount,
        selectPrice,
        clearCart,
        updateNewList,
    }
}, {
    persist: true,
})